package com.krm.codemanager.service;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.krm.codemanager.mapper.MetaStandMapper;
import com.krm.codemanager.model.MetaStandard;

import utils.CommonEntity;
import utils.ExcelBean;
import utils.ExcelUtils;

/**
 * @Title: ReportService.java
 * @Package com.krm.web.design.service
 * @Description: TODO(用一句话描述该文件做什么)
 * @author: yys
 * @date: 2018年6月11日 下午2:26:10
 * @version report V2.0
 */
@Service("metaStandService")
public class MetaStandService {

	@Resource
	private MetaStandMapper metaStandMapper;

	public PageInfo<CommonEntity> pageInfo(Map<String, Object> params) {
		PageHelper.startPage(params);
		List<CommonEntity> list = metaStandMapper.pageInfo(params);
		return new PageInfo<CommonEntity>(list);
	}
	
	public List<MetaStandard> entiyList(Map<String, Object> params){
		List<MetaStandard> entityList = new ArrayList<>();
		List<CommonEntity> list = metaStandMapper.pageInfo(params);
		for (CommonEntity commonEntity : list) {
			entityList.add(JSON.toJavaObject(commonEntity, MetaStandard.class));
		}
		return entityList;
	}
	
    public int addMetaStand(MetaStandard metaStandard){
    	return metaStandMapper.addMetaStand(metaStandard);
    }
	
	public int updateMetaStand(MetaStandard metaStandard){
		return metaStandMapper.updateMetaStand(metaStandard);
	}
	
	public int delMetaStand(Integer fieldId){
		return metaStandMapper.delMetaStand(fieldId);
	}

	public void insertBatch(List<MetaStandard> importExcel) {
		// TODO Auto-generated method stub
		metaStandMapper.insertBatch(importExcel);
	}

	public List queryMetaStand(String createUser) {
		// TODO Auto-generated method stub
		return metaStandMapper.queryMetaStand(createUser);
	}
	
	public String uploadPayerCreditInfoExcel(InputStream in, MultipartFile file) throws Exception {
		// TODO Auto-generated method stub
		List<List<Object>> listob = ExcelUtils.getBankListByExcel(in, file.getOriginalFilename());
		//List<MetaStandard> creditInfoList = new ArrayList<MetaStandard>();
		int j = 0;
		int k = 0;
		for (int i = 0; i < listob.size(); i++) {
			List<Object> ob = listob.get(i);
			MetaStandard creditInfoBean = new MetaStandard();
			if( ob.get(0)!=null && !ob.get(0).equals("")){
				creditInfoBean.setStdMetaName(String.valueOf(ob.get(0)));
			}
			if( ob.get(1)!=null && !ob.get(1).equals("")){
				creditInfoBean.setStdMetaChnName(String.valueOf(ob.get(1)));
			}
			if( ob.get(2)!=null && !ob.get(2).equals("")){
				creditInfoBean.setStdMetaType(String.valueOf(ob.get(2)));
			}
			if( ob.get(3)!=null && !ob.get(3).equals("")){
				creditInfoBean.setStdMetaLength(Integer.parseInt(String.valueOf(ob.get(3))));
			}else{
				creditInfoBean.setStdMetaLength(null);
			}
			if( ob.get(4)!=null && !ob.get(4).equals("")){
				creditInfoBean.setStdMetaPrecision(Integer.parseInt(String.valueOf(ob.get(4))));
			}else{
				creditInfoBean.setStdMetaPrecision(null);
			}
			if( ob.get(5)!=null && !ob.get(5).equals("")){
				creditInfoBean.setValidFlag(String.valueOf(ob.get(5)));
			}
			if( ob.get(6)!=null && !ob.get(6).equals("")){
				creditInfoBean.setCreateTime(String.valueOf(ob.get(6)));
			}
			if( ob.get(7)!=null && !ob.get(7).equals("")){
				creditInfoBean.setModefyTime(String.valueOf(ob.get(7)));
			}
			if( ob.get(8)!=null && !ob.get(8).equals("")){
				creditInfoBean.setCreatorName(String.valueOf(ob.get(8)));
			}
			if( ob.get(9)!=null && !ob.get(9).equals("")){
				creditInfoBean.setMaintainerName(String.valueOf(ob.get(9)));
			}
			MetaStandard onlyone = metaStandMapper.getOnlyone(creditInfoBean);
			if(onlyone!=null){
				j=j+1;
			}else{
				metaStandMapper.addMetaStand(creditInfoBean);
				k=k+1;
			}
			
			//creditInfoList.add(creditInfoBean);
		}
		String msg = "导入成功"+k+"条，重复数据"+j+"条";
		return msg;

	}

	public XSSFWorkbook exportPayerCreditInfoExcel(String user) {
		List<MetaStandard> creditInfoList = queryMetaStand(user);
		List<ExcelBean> ems = new ArrayList<>();
		Map<Integer, List<ExcelBean>> map = new LinkedHashMap<>();
		XSSFWorkbook book = null;
		ems.add(new ExcelBean("字段名称", "stdMetaName", 0));
		ems.add(new ExcelBean("字段中文名称", "stdMetaChnName", 0));
		ems.add(new ExcelBean("字段类型", "stdMetaType", 0));
		ems.add(new ExcelBean("字段长度", "stdMetaLength", 0));
		ems.add(new ExcelBean("字段精度", "stdMetaPrecision", 0));
		ems.add(new ExcelBean("有效标识", "validFlag", 0));
		ems.add(new ExcelBean("创建时间", "createTime", 0));
		ems.add(new ExcelBean("修改时间", "modefyTime", 0));
		ems.add(new ExcelBean("创建人", "creatorName", 0));
		ems.add(new ExcelBean("维护人", "maintainerName", 0));
		map.put(0, ems);
		try {
			book = ExcelUtils.createExcelFile(MetaStandard.class, creditInfoList, map, "应付账款信息");
		} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | ClassNotFoundException
				| IntrospectionException | ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return book;
	}

	public MetaStandard getOnlyone(MetaStandard metaStandard) {
		// TODO Auto-generated method stub
		return metaStandMapper.getOnlyone(metaStandard);
	}
}
